<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("conn.php");
require_once 'common.php';

$action = getParameter("action");

if ($action == "info") {
    $db = new DBC();
    $id = getParameter("id");
    $position = getParameter("position");
    $image_file = $_FILES ['imageURL'] ['name'];
    if ($image_file != "") {
        $path_parts = pathinfo($image_file);
        $imageURL = "search/" . date("YmdHis") . "." . $path_parts ['extension'];
        move_uploaded_file($_FILES ['imageURL'] ['tmp_name'], $imageURL);
    }
    $clear = false;
    if (isset($_POST['clear']) && $_POST['clear'] == "on") {
        $clear = true;
    }
    if ($_FILES ['imageURL'] ['name'] != "") {
        $sql = "SELECT * FROM t_search_info WHERE id = " . $id;
        $data = $db->fetchData($sql);
        if ($data != false && count($data) > 0) {
            $oldImageURL = $data[0]['imageURL'];
            if ($oldImageURL != "") {
                unlink($oldImageURL);
            }
        }
        if ($clear) {
            $sql = "UPDATE t_search_info SET position = '" . $position . "',imageURL='" . $imageURL . "',updateTime = now(),click = 0 WHERE id = " . $id;
        } else {
            $sql = "UPDATE t_search_info SET position = '" . $position . "',imageURL='" . $imageURL . "',updateTime = now() WHERE id = " . $id;
        }
    } else {
        if ($clear) {
            $sql = "UPDATE t_search_info SET position = '" . $position . "',updateTime = now(),click = 0 WHERE id = " . $id;
        } else {
            $sql = "UPDATE t_search_info SET position = '" . $position . "',updateTime = now() WHERE id = " . $id;
        }
    }
//    $transfer = "searchList.php?menu=search";
    $transfer = "searchEdit.php?menu=search&id=" . $id;
    if ($db->execute($sql)) {
        //success
        //    echo "<script language='javascript'>alert('成功'); </ script>";
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        //failue
        echo "<script language='javascript'>alert('失败'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
} else if ($action == "detail") {
    $db = new DBC();
    $infoID = getParameter("infoID");
    $code = getParameter("code");
    $sql = "SELECT * FROM t_product WHERE flashcode = '" . $code . "' AND status = 1";
    $data = $db->fetchData($sql);
    if (checkArrayNotEmpty($data)) {
        $sql = "INSERT INTO t_search_detail(code,name,click,infoID,updateTime) VALUES('" . $code . "','" . $data[0]['name'] . "',0," . $infoID . ",now())";
        $transfer = "searchEdit.php?menu=search&id=" . $infoID;
        if ($db->execute($sql)) {
            //success
            //    echo "<script language='javascript'>alert('成功'); </ script>";
            echo "<script language='javascript'>location='" . $transfer . "';</script>";
        } else {
            //failue
            echo "<script language='javascript'>alert('失败'); </script>";
            echo "<script language='javascript'>history.back();</script>";
        }
    } else {
        echo "<script language='javascript'>alert('无此编号：" . $code . "'); </script>";
        echo "<script language='javascript'>history.back();</script>";
    }
}
?>
